# -*- coding: utf-8 -*-

# -------------------------------
# Author    : linlingwei
# Email     : 951035650@qq.com
# File Name : jointTool.py
# Date      : 2018/11/7
# IDE       : PyCharm
# Version   : 1.0.1
# -------------------------------

import copy,time

import maya.cmds as mc

import configs.constants.attribute_constant as ac
import configs.constants.nodeType_constant as nc
import configs.constants.suffix_constant as sc
import configs.constants.body_constant as bc
import configs.constants.global_constant as gc

import scripts.commonCmds as con



def joint(obj, name, parent=None,suffix=False):
    
    # objTrans = mc.xform(obj,q=True,ws=True,t=True)
    if suffix:
        name = con.lname(name,sc.sJot)
    jot = mc.createNode(nc.tJoint,name = name,ss=True)
    mc.delete(mc.pointConstraint(obj,jot,mo=False))
    if parent:
        mc.parent(jot,parent)
    # time.sleep(0.02)
    # mc.refresh()
    return jot



def jointChain(jotlist,reverse=False,orient=True,axial = "y"):
    jotlistCopy = copy.copy(jotlist)
    if reverse:
        jotlistCopy.reverse()
    # print(jotlistCopy[::-1][:-1])
    for i,jot in enumerate(jotlistCopy[::-1][:-1]):
        # print(jot,jotlistCopy[i+1])
        mc.parent(jot,jotlistCopy[::-1][i+1])
    
    if axial == 'y':
        mc.joint(jotlistCopy, e=True, oj='yzx', secondaryAxisOrient='zup', ch=True, zso=True)
    elif axial == 'x':
        mc.joint(jotlistCopy, e=True, oj='xyz', secondaryAxisOrient='yup', ch=True, zso=True)
        
    if orient:
        mc.setAttr('%s.jointOrient' % (jotlistCopy[-1]), 0, 0, 0)

    return

def seqJointChain(seqDict,name):
    
    seqs = seqDict[sc.gSeq]
    jotlist = list()
    for seq in seqs:
        jot = joint(seqDict[seq], name=con.lname(name,seq))
        jotlist.append(jot)
    jointChain(jotlist)
    
    return jotlist

def crvJointChain(crv,number,name,suffix=False):
    
    crvShape = con.findRelatedShape(crv)
    
    pro = 1.0/(number-1)
    jotlist = list()
    pat = None
    for i in range(number):
        poci = mc.createNode(nc.tPointOnCurveInfo,name = "temp%s"%i)
        mc.connectAttr(crvShape+".ws[0]",poci+".ic")
        mc.setAttr(poci+".top",1)
        mc.setAttr(poci+".pr",pro*i)
        
        pt = mc.getAttr(poci+".p")[0]

        if suffix:
            nn = con.lname(name+str(i+1),sc.sJot)
        else:
            nn = con.lname(name+str(i+1))
        jot = mc.createNode(nc.tJoint,name=nn)
        mc.setAttr(jot+".t",pt[0],pt[1],pt[2])
        
        if pat:
            mc.parent(jot,pat)
        pat = jot
        
        jotlist.append(jot)

        # time.sleep(0.02)
        # mc.refresh()

    mc.joint(jotlist, e=True, oj='xyz', secondaryAxisOrient='yup', ch=True, zso=True)
    mc.setAttr('%s.jointOrient' % (jotlist[-1]), 0, 0, 0)
    return jotlist

def noroll(star,end,name,axial = "y"):
    localname = con.lname(name,"noroll")

    sjot = joint(star,con.lname(localname,"star"))

    ejot = joint(end,con.lname(localname,'end'))
    
    jointChain([sjot,ejot],axial=axial)
    
    starPat = mc.listRelatives(star,p=True)
    mc.parent(sjot,starPat)

    ikh = mc.ikHandle(sj=sjot,ee=ejot,sol="ikRPsolver",name=con.lname(localname,sc.sIkhandle))[0]

    mc.parent(ikh,star)
    mc.poleVectorConstraint(sjot,ikh)

    nrjot = joint(star,con.lname(localname,"out"))
    mc.parent(nrjot,sjot)
    mc.setAttr('%s.jointOrient' % nrjot, 0, 0, 0)

    mc.parentConstraint(star,nrjot,mo=True)
    mc.parentConstraint(starPat,sjot,mo=True)
    
    # cCore.addEmptyGroup(sjot,["Zero"])
    mc.hide(ikh,sjot)

    return nrjot,sjot
#
if __name__ == '__main__':
    pass